import Vue from 'vue'
import axios from 'axios'
import qs from 'qs'
axios.defaults.baseURL = 'http://dinglearning.eduto.com/fuNeng/';
Vue.mixin({
    methods: {
        $_post(url, data) {
            let _self = this;
            return new Promise((resolve, reject) => {
                axios.post(url, qs.stringify(data)).then(res => {
                    if (res.data.status == 'ok') {
                        resolve(res.data);
                    } else {
                        console.log({
                            message: res.data.message,
                            position: 'middle',
                            duration: 3000
                        });
                    }
                }).catch(function(error) {
                    let errorData = error.response.data;
                    if (errorData.code != 401) {
                        if(errorData.message){
                            console.log({
                                message: errorData.message,
                                position: 'middle',
                                duration: 3000
                            });
                        }
                    }
                })
            })
        },
        $_get(url, data) {
            let _self = this;
            return new Promise((resolve, reject) => {
                axios.get(url, {
                    params: data

                }).then(res => {
                    if (res.data.status == 'ok') {
                        resolve(res.data);
                    } else {
                        console.log({
                            message: res.data.message,
                            position: 'middle',
                            duration: 3000
                        });
                    }
                }).catch(function(error) {
                    let errorData = error.response.data;
                    if (errorData.code != 401) {
                        if(errorData.message){
                            console.log({
                                message: errorData.message,
                                position: 'middle',
                                duration: 3000
                            });
                        }
                    }
                })
            })
        },
        $_delete(url) {
            let _self = this;
            return new Promise((resolve, reject) => {
                axios.delete(url).then(res => {
                    if (res.data.status == 'ok') {
                        resolve(res.data);
                    } else {
                        console.log({
                            message: res.data.message,
                            position: 'middle',
                            duration: 3000
                        });
                    }
                }).catch(function(error) {
                    let errorData = error.response.data;
                    if (errorData.code != 401) {
                        if(errorData.message){
                            console.log({
                                message: errorData.message,
                                position: 'middle',
                                duration: 3000
                            });
                        }
                    }
                })
            })
        },
        $_put(url, data) {
            let _self = this;
            return new Promise((resolve, reject) => {
                axios.put(url,qs.stringify(data)).then(res => {
                    if (res.data.status == 'ok') {
                        resolve(res.data);
                    } else {
                        console.log({
                            message: res.data.message,
                            position: 'middle',
                            duration: 3000
                        });
                    }
                }).catch(function(error) {
                    let errorData = error.response.data;
                    if (errorData.code != 401) {
                        if(errorData.message){
                            console.log({
                                message: errorData.message,
                                position: 'middle',
                                duration: 3000
                            });
                        }
                    }
                })
            })
        },
    },
})
